7.List Comprehensions(理解串列) <<
Previous Next >> 17.Decode a web page(解碼網站)
8.Rock paper sciorrs(剪刀石頭布)
Kaggle:https://www.kaggle.com/gg542466/hw3-8/edit
Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
請做出有兩個玩家的猜拳遊戲,(提示:要求可以與玩家互動(使用input),並比較它們,列印出恭喜勝利者的訊息,然後詢問玩家是否想要開始新的遊戲)。
Remember the rules:
編碼的規則:
1.Rock beats scissors
石頭贏剪刀
2.Scissors beats paper
剪刀贏布
3.Paper beats rock
布贏石頭
Discussion(討論)
Concepts for this week:
這周會用到的概念有:
1.While loops
2.Infinite loops
3.Break statements(拆解陳述)
While loops
We have already discussed for loops, or loops that look sequentially (one by one) at elements in a list. There is a second type of loop that works in a slightly different way called a while loop.
我們已經討論過for循環,或循環(一個個看)列表中元素的循環。還有另一種類型的循環,其工作方式稍有不同,稱為while循環。
The idea is simple: while a certain condition is True, keep doing something. For example:
這個是個很簡單的想法:當某個條件為True時,就繼續做某事。例如:
a = 5 while (a > 0): print(a) a -= 1
The output of this code segment is
5
4
3
2
1
A particularly useful way to use while loops is checking user input for correctness. For example:
使用while循環的一種特別有用的方法是檢查用戶輸入的正確性。例如:
quit = input('Type "enter" to quit:' )
while quit != "enter":
quit = input('Type "enter" to quit:' )
The uses for this are infinite, and can (and should!) be combined with conditionals to yield the most efficient results.
此方法的用途是無限的,並且可以(和應該!)與條件條件結合使用以產生最有效的結果。
Infinite loops(無限循環)
An infinite loop is a loop that never stops. This means that the condition in the beginning of the while loop will always be true.
無限循環是永不停止的循環。這意味著while循環開始時的條件將始終為true。
For example:
舉個例子:
i = 5
while i > 0:
print("Inside the loop")
What will happen is the loop will print out the phrase “Inside the loop” forever and ever. If you are running your computer, you will have to “kill the program” to stop it. Each operating system has a different way of “killing a program” to get out of an infinite loop.
這樣做的話,循環將永遠打印出短語“ Inside the loop”。如果您正在運行計算機,則必須“殺死該程序”以將其停止。每個操作系統都有不同的“殺死程序”以擺脫無限循環的方式。
On Linux: in the terminal, type “CTRL-C” to kill the program that is currently running in the terminal. If you are using the IDLE Python IDE, then you must press “CTRL-D” to exit your running program. When in doubt, do a Google search before you start programming!
在Linux上:在終端中,鍵入“ CTRL-C”以終止終端中當前正在運行的程序。如果使用的是IDLE Python IDE,則必須按“ CTRL-D”退出正在運行的程序。如有疑問,請在開始編程之前進行Google搜索!
On Windows: type “CTRL-ALT-DEL” and open the task manager to kill the program.
在Windows上:鍵入“ CTRL-ALT-DEL”並打開任務管理器以終止程序。
On Mac: right-click on the task, and kill the program that is running forever.
在Mac上:右鍵單擊該任務,然後終止永久運行的程序。
If you find yourself in an infinite loop, your program will never end.
如果發現自己陷入無限循環,則程序將永遠不會結束。
Break statements(拆解陳述)
A break statement stops the execution of a loop before the original condition is met. While the use of a break statement will often start an argument about good coding practices, sometimes it is useful.
break語句會在滿足原始條件之前停止執行循環。儘管使用break語句通常會引發有關良好編碼實踐的爭論,但有時它很有用。
For example:
舉個例子:
while True:
usr_command = input("Enter your command: ")
if usr_command == "quit":
break
else:
print("You typed " + usr_command)
In this case, the break statement is used to break off the “infinite while loop” that we have constructed with the while True statement.
在這種情況下,break語句用於中斷我們使用while True語句構建的“ while循環無限”。
Happy coding!
編碼加油!!
個人答題結果:
import sys
PlayerA = input("pleace enter your name:")
PlayerB = input("and your name?:")
PlayerA_says = input("%s, Which chooses do you like playerA ?rock,paper or scissors?" % PlayerA)
PlayerB_says= input("%s, Now is your turn playerB rock,paper or scissors?" % PlayerB)
def compare(PlayerA, PlayerB):
if PlayerA == PlayerB:
return("It's a tie,pleace try again")
elif PlayerA == "rock":
if PlayerB == "scissors":
return("Congratulations!rock win!")
else:
return("Congratulations!paper win!")
elif PlayerA == "scissors":
if PlayerB == "paper":
return("Congratulations!scissors win!")
else:
return("Congratulations!rock win!")
elif PlayerA == "paper":
if PlayerB == "rock":
return("Congratulations!rock win!")
else:
return("Congratulations!scissors win!")
else:
return("error input! pleace enter rock,paper or scissors again!")
sys.exit()
print(compare(PlayerA_says, PlayerB_says))
7.List Comprehensions(理解串列) <<
Previous Next >> 17.Decode a web page(解碼網站)